<?php

	function smarty_block_ships($params, $content, &$smarty, &$repeat)
    {
		global $db, $config;

		$l = count($smarty->_tag_stack);
		if(empty($params['name'])) $params['name'] = "ships";

		if(empty($smarty->blockvars[$l]))
        {
			$sqlstr = "select * from {$config->db_prefix}ship where 1=1 ";

            if( isset($params['id']) && 0<(int)$params['id'])
                $sqlstr .= (" and id = ".(int)$params['id']);
				
			 if( isset($params['ship_id']) && 0<(int)$params['ship_id'])
                $sqlstr .= (" and id = ".(int)$params['ship_id']);
				
			if( isset($params['status']) )
                $sqlstr .= (" and status = ".(int)$params['status']);
			
			if( isset($params['ship_type_id']) && (int)$params['ship_type_id'] >= 0)
				$sqlstr .= (" and type=".(int)$params['ship_type_id']);
						
			$limit = empty($params['limit'])?9999:$params['limit'];
			$page = empty($params['page'])?1:$params['page'];
			$pager = new pager($limit, $page);
			$smarty->blockvars[$l] = $pager->execute($db, $sqlstr);
			if(!$smarty->blockvars[$l]) return $repeat = false;
			if(!empty($params['pager'])) $smarty->assign($params['pager'], $pager);
		}

		if(list($key, $item) = each($smarty->blockvars[$l])) {
			$repeat = true;
			$item['type_name'] = ship::getTypeName($item['type']);
			$port = port::getPort($item['ship_native_port_id']);
			$item['ship_native_port_name'] = $port ? $port['name'] : 'δ֪';
			$smarty->assign($params['name'], $item);
		}
		else
		{
			$repeat = false;
			reset($smarty->blockvars[$l]);
		}

		if(!is_null($content)) print $content;
		if(!$repeat) $smarty->blockvars[$l] = array();
	}
